package com.example.test_event_and_callback.infras;

import cn.hutool.core.thread.NamedThreadFactory;
import com.example.test_event_and_callback.infras.async.AsyncExecuteProxy;
import com.example.test_event_and_callback.infras.async.MethodCallType;
import org.reflections.Reflections;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Song Dan Yang
 * Description: </br>
 * date: 2023/11/22 8:39
 */

@Configuration
public class AppConfig {

    @Bean("serviceTaskPool")
    public ThreadPoolTaskExecutor serviceTaskPool1() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(8);
        executor.setMaxPoolSize(16);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(10);
        executor.setThreadNamePrefix("serviceTask-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }

    @Bean
    public Reflections reflections(){
        return new Reflections("com.example.test_event_and_callback");
    }

    @Bean("AsyncExecuteProxy")
    public AsyncExecuteProxy AsyncExecuteProxy(){

        ThreadPoolExecutor taskPool = new ThreadPoolExecutor(
                8,
                16,
                3000,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(200),
                new NamedThreadFactory("ServiceThread-",false),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        return AsyncExecuteProxy.newBuilder()
                .setTaskPool(taskPool)
                .setMethodCallType(MethodCallType.METHOD_HANDLE)
                .build();
    }


    public ThreadPoolTaskExecutor serviceTaskPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(8);
        executor.setMaxPoolSize(16);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(10);
        executor.setThreadNamePrefix("serviceTask-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }





}
